﻿// ==================================================================================================================                                                                                          
//        ,::i                                                           BBB                
//       BBBBBi                                                         EBBB                
//      MBBNBBU                                                         BBB,                
//     BBB. BBB     BBB,BBBBM   BBB   UBBB   MBB,  LBBBBBO,   :BBG,BBB :BBB  .BBBU  kBBBBBF 
//    BBB,  BBB    7BBBBS2BBBO  BBB  iBBBB  YBBJ :BBBMYNBBB:  FBBBBBB: OBB: 5BBB,  BBBi ,M, 
//   MBBY   BBB.   8BBB   :BBB  BBB .BBUBB  BB1  BBBi   kBBB  BBBM     BBBjBBBr    BBB1     
//  BBBBBBBBBBBu   BBB    FBBP  MBM BB. BB BBM  7BBB    MBBY .BBB     7BBGkBB1      JBBBBi  
// PBBBFE0GkBBBB  7BBX   uBBB   MBBMBu .BBOBB   rBBB   kBBB  ZBBq     BBB: BBBJ   .   iBBB  
//BBBB      iBBB  BBBBBBBBBE    EBBBB  ,BBBB     MBBBBBBBM   BBB,    iBBB  .BBB2 :BBBBBBB7  
//vr7        777  BBBu8O5:      .77r    Lr7       .7EZk;     L77     .Y7r   irLY  JNMMF:    
//               LBBj
//
// Apworks Application Development Framework
// Copyright (C) 2010-2015 by daxnet.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==================================================================================================================

using System;

namespace Apworks.Config.Fluent
{
    /// <summary>
    /// Represents that the implemented classes are exception handler configurators.
    /// </summary>
    public interface IExceptionHandlerConfigurator : IConfigSourceConfigurator { }

    /// <summary>
    /// Represents the exception handler configurator.
    /// </summary>
    public class ExceptionHandlerConfigurator : ConfigSourceConfigurator, IExceptionHandlerConfigurator
    {
        #region Private Fields
        private readonly Type exceptionType;
        private readonly Type exceptionHandlerType;
        private readonly ExceptionHandlingBehavior behavior;
        #endregion

        #region Ctor
        /// <summary>
        /// Initializes a new instance of <c>ExceptionHandlerConfigurator</c> class.
        /// </summary>
        /// <param name="context">The configuration context.</param>
        /// <param name="exceptionType">The type of the exception to be handled.</param>
        /// <param name="exceptionHandlerType">The type of the exception handler.</param>
        /// <param name="behavior">The exception handling behavior.</param>
        public ExceptionHandlerConfigurator(IConfigSourceConfigurator context, Type exceptionType, Type exceptionHandlerType, ExceptionHandlingBehavior behavior)
            : base(context)
        {
            this.exceptionType = exceptionType;
            this.exceptionHandlerType = exceptionHandlerType;
            this.behavior = behavior;
        }
        /// <summary>
        /// Initializes a new instance of <c>ExceptionHandlerConfigurator</c> class.
        /// </summary>
        /// <param name="context">The configuration context.</param>
        /// <param name="exceptionType">The type of the exception to be handled.</param>
        /// <param name="exceptionHandlerType">The type of the exception handler.</param>
        public ExceptionHandlerConfigurator(IConfigSourceConfigurator context, Type exceptionType, Type exceptionHandlerType)
            : this(context, exceptionType, exceptionHandlerType, ExceptionHandlingBehavior.Direct)
        { }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Configures the container.
        /// </summary>
        /// <param name="container">The configuration container.</param>
        /// <returns>The configured container.</returns>
        protected override RegularConfigSource DoConfigure(RegularConfigSource container)
        {
            container.AddException(this.exceptionType, behavior);
            container.AddExceptionHandler(this.exceptionType, this.exceptionHandlerType);
            return container;
        }
        #endregion
    }
}
